package com.xiaobawang.xjsyy_idea_plugin.panel.dev_process.view;

import com.xiaobawang.xjsyy_idea_plugin.panel.dev_process.struct.*;
import com.xiaobawang.xjsyy_idea_plugin.service.Service;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.*;
import java.util.List;

/**
 * @author synhard
 * @description TODO
 * @date 2024/5/16 14:57
 * @email songxinhang2023@e-chinalife.com
 * @id 18101119
 * @phone 13001321080
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class DevProcessFrame extends JPanel implements ActionListener {
    private static final Logger log = LoggerFactory.getLogger(DevProcessFrame.class);
    // 卡片区域
    private JPanel cardPanel;
    // 负责切换卡片的按钮区域
    private JPanel buttonPanel;
    // 定义卡片布局
    private CardLayout cardLayout;
    // 定义视图绑定的研发流程
    private List<DevProcess> devProcessList;
    // 按钮名称 -> 按钮
    private Map<String, JButton> buttonMap;

    public DevProcessFrame() {
        this.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                // ...
                log.info(e.toString());
            }

            @Override
            public void focusLost(FocusEvent e) {
                // ...
                log.info(e.toString());
            }
        });
    }

    public void init() {
        // 按钮map
        this.buttonMap = new HashMap<>();
        // 显示区域
        this.cardPanel = new JPanel();
        // 按钮
        this.buttonPanel = new JPanel();
        // 卡片布局
        this.cardLayout = new CardLayout();
        this.cardPanel.setLayout(cardLayout);
        // 视图绑定的实体对象
        this.devProcessList = Service.releaseWorks(); // TODO 从云效获取数据
//        this.devProcessList = buildDataExample();

        for (int i = 0; i < devProcessList.size(); i++) {
            DevProcess devProcess = devProcessList.get(i);
            DevProcessPanel devProcessPanel = new DevProcessPanel(devProcess);
            cardPanel.add(devProcess.getName(), devProcessPanel);
            JButton btn = new JButton(devProcess.getName());
            btn.addActionListener(new CardSwitcher(devProcess.getName()));
            buttonMap.put(devProcess.getName(), btn);
            buttonPanel.add(btn);
            if (i == 0) {
                btn.setEnabled(false);
                btn.requestFocus();
            }
        }
        setLayout(new BorderLayout());
        add(buttonPanel, BorderLayout.NORTH); // 按钮组件放到上面
        add(cardPanel, BorderLayout.CENTER); // 文本域组件放到中间
    }

    private class CardSwitcher implements ActionListener {
        private final String cardName;

        public CardSwitcher(String cardName) {
            this.cardName = cardName;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            // 切换卡片
            cardLayout.show(cardPanel, cardName);
            // 控制按钮状态
            for (Map.Entry<String, JButton> entry : buttonMap.entrySet()) {
                if (entry.getKey().equals(cardName)) {
                    entry.getValue().setEnabled(false);
                    entry.getValue().requestFocus();
                } else {
                    entry.getValue().setEnabled(true);
                }
            }
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        cardLayout.show(cardPanel, e.getActionCommand());
    }

    private List<DevProcess> buildDataExample() {
        // 研发流程数据实体
        List<DevProcess> devProcessList = new ArrayList<>();

        DevProcess buildDeploy = buildDevelop();
        DevProcess putTag = putTag();
        devProcessList.add(buildDeploy);
        devProcessList.add(putTag);

        return devProcessList;
    }

    /**
     * @description 构造开发阶段对应的研发流程
     * @author synhard
     * @date 2024/5/16 14:22
     * @email songxinhang2023@e-chinalife.com
     * @id 18101119
     * @phone 13001321080
     */
    private DevProcess buildDevelop() {
        DevProcess develop = new DevProcess();
        develop.setName("开发阶段");
        develop.setReleaseId(UUID.randomUUID().toString());
        develop.setAppName("synhard_app");
        develop.setOrder(1);
        Pipeline developPipeline = new Pipeline();
        // 构造代码源
        List<CodeSource> developCodeSourceList = new ArrayList<>();
        CodeSource                 developCodeSource1    = new CodeSource();
        developCodeSource1.setName("synhard_app_web");
        developCodeSource1.setBranch("master");
        developCodeSource1.setType("codeUp3");
        developCodeSource1.setEndpoint("https://github.com/xiaobawang/xjsyy_idea_plugin.git");
        developCodeSourceList.add(developCodeSource1);
        CodeSource developCodeSource2 = new CodeSource();
        developCodeSource2.setName("synhard_app_service");
        developCodeSource2.setBranch("release");
        developCodeSource2.setType("codeUp5");
        developCodeSource2.setEndpoint("https://github.com/xiaobawang/xjsyy_idea_plugin.git");
        developCodeSourceList.add(developCodeSource2);
        // 构造具体阶段
        List<PipelineStage> developPipelineStageList = new ArrayList<>();
        // 阶段一
        PipelineStage developPipelineStage1 = new PipelineStage();
        developPipelineStage1.setName("准备");
        developPipelineStage1.setOrder(1);
        developPipelineStage1.setEndTime("2024-05-15 17:41:33");
        developPipelineStage1.setStartTime("2024-05-15 17:41:13");
        developPipelineStage1.setResultStatus("SUCCESS");
        developPipelineStage1.setStatus("FINISH");
        // 阶段一的任务
        List<PipelineStageJob> developPipelineStageJobList1 = new ArrayList<>();
        PipelineStageJob                 developPipelineStageJob1     = new PipelineStageJob();
        developPipelineStageJob1.setName("变量准备");
        developPipelineStageJob1.setOrder(1);
        developPipelineStageJob1.setStartTime("2024-05-15 17:41:33");
        developPipelineStageJob1.setEndTime("2024-05-15 17:41:53");
        developPipelineStageJob1.setResultStatus("SUCCESS");
        developPipelineStageJob1.setStatus("FINISH");
        developPipelineStageJobList1.add(developPipelineStageJob1);
        developPipelineStage1.setPipelineStageJobList(developPipelineStageJobList1);
        developPipelineStageList.add(developPipelineStage1);

        // 阶段二
        PipelineStage    developPipelineStage2 = new PipelineStage();
        developPipelineStage2.setName("获取项目环境");
        developPipelineStage2.setOrder(2);
        developPipelineStage2.setStartTime("2024-05-15 17:41:13");
        developPipelineStage2.setEndTime("2024-05-15 17:41:33");
        developPipelineStage2.setResultStatus("SUCCESS");
        developPipelineStage2.setStatus("FINISH");
        // 阶段二的任务
        List<PipelineStageJob> developPipelineStageJobList2 = new ArrayList<>();
        PipelineStageJob                 developPipelineStageJob2     = new PipelineStageJob();
        developPipelineStageJob2.setName("获取项目环境");
        developPipelineStageJob2.setOrder(1);
        developPipelineStageJob2.setStartTime("2024-05-15 17:41:33");
        developPipelineStageJob2.setEndTime("2024-05-15 17:41:53");
        developPipelineStageJob2.setResultStatus("SUCCESS");
        developPipelineStageJob2.setStatus("FINISH");
        developPipelineStageJobList2.add(developPipelineStageJob2);
        developPipelineStage2.setPipelineStageJobList(developPipelineStageJobList2);
        developPipelineStageList.add(developPipelineStage2);

        // 阶段三
        PipelineStage    developPipelineStage3 = new PipelineStage();
        developPipelineStage3.setName("构建");
        developPipelineStage3.setOrder(3);
        developPipelineStage3.setStartTime("2024-05-15 17:41:13");
        developPipelineStage3.setEndTime("2024-05-15 17:41:33");
        developPipelineStage3.setResultStatus("SUCCESS");
        developPipelineStage3.setStatus("FINISH");
        // 阶段三的任务
        List<PipelineStageJob> developPipelineStageJobList3 = new ArrayList<>();
        PipelineStageJob                 developPipelineStageJob3     = new PipelineStageJob();
        developPipelineStageJob3.setName("jar包构建");
        developPipelineStageJob3.setOrder(1);
        developPipelineStageJob3.setStartTime("2024-05-15 17:41:33");
        developPipelineStageJob3.setEndTime("2024-05-15 17:41:53");
        developPipelineStageJob3.setResultStatus("SUCCESS");
        developPipelineStageJob3.setStatus("FINISH");
        PipelineStageJob developPipelineStageJob4           = new PipelineStageJob();
        developPipelineStageJob4.setName("war包构建");
        developPipelineStageJob4.setOrder(2);
        developPipelineStageJob4.setStartTime("2024-05-15 17:41:33");
        developPipelineStageJob4.setEndTime("2024-05-15 17:41:53");
        developPipelineStageJob4.setResultStatus("SUCCESS");
        developPipelineStageJob4.setStatus("FINISH");
        developPipelineStageJobList3.add(developPipelineStageJob3);
        developPipelineStageJobList3.add(developPipelineStageJob4);
        developPipelineStage3.setPipelineStageJobList(developPipelineStageJobList3);
        developPipelineStageList.add(developPipelineStage3);

        // 阶段二
        PipelineStage    developPipelineStage4 = new PipelineStage();
        developPipelineStage4.setName("应用部署");
        developPipelineStage4.setOrder(4);
        developPipelineStage4.setStartTime("2024-05-15 17:41:13");
        developPipelineStage4.setEndTime("2024-05-15 17:41:33");
        developPipelineStage4.setResultStatus("SUCCESS");
        developPipelineStage4.setStatus("FINISH");
        // 阶段二的任务
        List<PipelineStageJob> developPipelineStageJobList4 = new ArrayList<>();
        PipelineStageJob                 developPipelineStageJob5     = new PipelineStageJob();
        developPipelineStageJob5.setName("部署");
        developPipelineStageJob5.setOrder(1);
        developPipelineStageJob5.setStartTime("2024-05-15 17:41:33");
        developPipelineStageJob5.setEndTime("2024-05-15 17:41:53");
        developPipelineStageJob5.setResultStatus("SUCCESS");
        developPipelineStageJob5.setStatus("FINISH");
        developPipelineStageJobList4.add(developPipelineStageJob5);
        developPipelineStage4.setPipelineStageJobList(developPipelineStageJobList4);
        developPipelineStageList.add(developPipelineStage4);

        developPipeline.setPipelineStageList(developPipelineStageList);
        developPipeline.setCodeSourceList(developCodeSourceList);
        develop.setPipeline(developPipeline);
        return develop;
    }

    /**
     * @description 构造打 Tag 对应的研发流程
     * @author synhard
     * @date 2024/5/16 14:22
     * @email songxinhang2023@e-chinalife.com
     * @id 18101119
     * @phone 13001321080
     */
    private DevProcess putTag() {
        DevProcess putTag = new DevProcess();
        putTag.setName("打tag");
        putTag.setReleaseId(UUID.randomUUID().toString());
        putTag.setAppName("synhard_app");
        putTag.setOrder(2);
        Pipeline putTagPipeline = new Pipeline();
        // 构造代码源
        List<CodeSource> developCodeSourceList = new ArrayList<>();
        CodeSource                 developCodeSource1    = new CodeSource();
        developCodeSource1.setName("synhard_app_web");
        developCodeSource1.setBranch("master");
        developCodeSource1.setType("codeUp3");
        developCodeSource1.setEndpoint("https://github.com/xiaobawang/xjsyy_idea_plugin.git");
        developCodeSourceList.add(developCodeSource1);
        // 构造具体阶段
        List<PipelineStage> developPipelineStageList = new ArrayList<>();
        // 阶段一
        PipelineStage developPipelineStage1 = new PipelineStage();
        developPipelineStage1.setName("阶段一");
        developPipelineStage1.setOrder(1);
        developPipelineStage1.setEndTime("2024-05-15 17:41:33");
        developPipelineStage1.setStartTime("2024-05-15 17:41:13");
        developPipelineStage1.setResultStatus("SUCCESS");
        developPipelineStage1.setStatus("FINISH");
        // 阶段一的任务
        List<PipelineStageJob> developPipelineStageJobList1 = new ArrayList<>();
        PipelineStageJob       developPipelineStageJob1     = new PipelineStageJob();
        developPipelineStageJob1.setName("空任务");
        developPipelineStageJob1.setOrder(1);
        developPipelineStageJob1.setStartTime("2024-05-15 17:41:33");
        developPipelineStageJob1.setEndTime("2024-05-15 17:41:53");
        developPipelineStageJob1.setResultStatus("SUCCESS");
        developPipelineStageJob1.setStatus("FINISH");
        developPipelineStageJobList1.add(developPipelineStageJob1);
        developPipelineStage1.setPipelineStageJobList(developPipelineStageJobList1);
        developPipelineStageList.add(developPipelineStage1);
        putTagPipeline.setCodeSourceList(developCodeSourceList);
        putTagPipeline.setPipelineStageList(developPipelineStageList);
        putTag.setPipeline(putTagPipeline);
        return putTag;
    }
}
